import api from '../../../api/index'
import util from "../../../utils/util"
import CONFIG from '../../../utils/config';

const app = getApp()
let shareData = null
Page({
    data: {
        show: false,
        src: null
    },
    /**
     * 打开webview页
     * */
    async onLoad(options) {
        wx.showLoading({
            title: '加载中'
        })
        console.log('222222');
        console.log(wx.getStorageSync('accessToken'));
        if( wx.getStorageSync('accessToken')) {
            this.getTicket()
        } else {
            // this.getTicket()
            this.getorg()
        }
        
    },
    getorg () {
        const actions = {
          data: {},
          head: {}
        };
        try {
          api.get(`/api/hxhealth/login/orgapp/${CONFIG.orgCode}/${CONFIG.appCode}`,actions,true).then((res) => {
            console.log(res);
            console.log(res.head['Set-Cookie']);
            if (res.head['Set-Cookie']) {
              wx.setStorageSync('accessToken', res.head['Set-Cookie']);
            } 
            this.getTicket()
          });
        } catch (e) {
          console.error(e);
        }
      },
    async getTicket() {
        try {
            // await app.isLogin({ctx: this})
            const list = await api.post('/api/hxhealth/user/laikangticket')
            console.log(list);
            if(wx.getStorageSync('LKaccessToken')) {
                const LKaccessToken = wx.getStorageSync('LKaccessToken')
                let extractedString = LKaccessToken.match(/JSESSIONID=([^;]+)/);
                let result = extractedString[1]; // 获取匹配到的子字符串
                console.log(result); // 输出提取的子字符串
                this.setData({
                    src: `${list[0].url}/lktoolsh5/research-wx/?cookie=${result}#/small_coup/index/index`,
                    // src: `https://mirror.laikang.com/lktoolsh5/research-wx/?cookie=YWQwOTczMTgtZWIwNS00N2FmLTg1MzMtNGU2YzAyNmNlNDkw#/small_coup/index/index`,
                    // src: `https://mirror-test.v.laikang.com/lktoolsh5/research-wx/?#/subPages/login/index`,
                    show: true
                })
                console.log(this.data.src);
                 
            } else {
                this.getTickettologin(list[0].url,list[0].ticket)
            }
            
        console.log(haha);
            return list
        } catch (e) {
            return null
        }
    },
    async getTickettologin(url,ticketvalue) {
        try {
            // await app.isLogin({ctx: this})
            const res = await api.ApiCookieGet(`${url}/api/login/ticket/${ticketvalue}?`)
            console.log(res);
            console.log(res.head['Set-Cookie']);
            const result = res.head['Set-Cookie']
            wx.setStorageSync('LKaccessToken',res.head['Set-Cookie'])
            let extractedString = result.match(/JSESSIONID=([^;]+)/);
                if (extractedString) {
                    let result = extractedString[1]; // 获取匹配到的子字符串
                    console.log(result); // 输出提取的子字符串
                    this.setData({
                        src: `${url}/lktoolsh5/research-wx/?cookie=${result}#/subPages/login/index`,
                        show: true
                    })
                    console.log(this.data.src);
                  } else {
                    console.log('未找到匹配的内容');
                  }
            return res
        } catch (e) {
            return null
        }
    },
    webLoadSuccess(e) {
        console.log(e)
        wx.hideLoading()
    },
    webLoadError() {
        wx.hideLoading()
    },
    onShareAppMessage({webViewUrl}) {
        let obj = {
            title: decodeURIComponent(this.options.title),
            url: webViewUrl
        }
        let data = {
            path: `${this.route}?${util.queryStringify(obj)}`
        }
        if (shareData) {
            const {title, path, pathType, imageUrl} = shareData
            title ? data.title = shareData.title : true
            imageUrl ? data.imageUrl = shareData.imageUrl : true
            if (path) {
                if (pathType === 'miniPath') {
                    data.path = path
                } else {
                    obj.url = path
                    data.path = `${this.route}?${util.queryStringify(obj)}`
                }
            }
        }
        console.log(data)
        return data
    },
    handlerMessage(e) {
        console.log(e.detail.data)
        for (let item of e.detail.data) {
            // 获取web中发送的分享图数据
            if (typeof item === 'object') {
                if (typeof shareData === 'object') {
                    shareData = item.shareData
                }
            }
        }
    }
})
